package com.dd.ent.car.ui.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.comment.Comment;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.common.xrichtext.ui.NoteActivity;
import com.dd.ent.car.service.comment.CommentService;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.ui.BaseActivity;
import com.dd.ent.car.ui.user.LoginActivity;
import com.dd.ent.car.ui.widget.CommentDialog;
import com.pp.ent.base.log.MLog;
import com.pp.ent.view.annotation.ContentView;
import com.pp.ent.view.annotation.Inject;
import com.pp.ent.view.annotation.OnClick;
import com.pp.ent.view.annotation.ViewInject;

import static com.dd.ent.car.ui.detail.DetailFragmentImp.FULL_TEXT_HEADER;
import static com.dd.ent.car.ui.detail.DetailFragmentImp.INTRIGUE_HEADER;
import static com.dd.ent.car.ui.detail.DetailFragmentImp.PIECE_HEADER;
import static com.dd.ent.car.ui.detail.DetailFragmentImp.VIDEO_HEADER;
import static com.pp.ent.core.manage.CoreManager.getContext;

/**
 * Created by dengqu on 2016/8/29.
 */
@ContentView(R.layout.activity_detail)
public class DetailActivity extends BaseActivity implements DetailCommentView.OnCommentItemListener, DetailBottomView.SendCommentListener {
    private final static String TAG = DetailActivity.class.getSimpleName();
    @ViewInject(R.id.detail_bottom_view)
    private DetailBottomView detailBottomView;
    @ViewInject(R.id.detail_header_view)
    private FrameLayout headerContainer;
    @Inject
    private CommentService commentService;

    @Inject
    private UserService userService;

    private DetailFragmentImp mDetailFragment;
    private int viewType;
    private Video video;
    private CommentDialog mCommentDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent();
        addDetailFragment();
        init();
    }

    private void init() {
        detailBottomView.setSendCommentListener(this);
        headerContainer.addView(getHeaderLayout());
    }


    @Override
    public void onSend(CharSequence content) {
        onSend(content, 0, null);
    }

    public void onSend(CharSequence content, int position, Comment originComment) {
        String originCommentId = originComment == null ? "" : originComment.getUuid();
        Comment comment = new Comment();
        comment.setUser(userService.getUser());
        comment.setContent(content.toString());
        comment.setOriginComment(originComment);
        comment.setCreateTime(System.currentTimeMillis());  
        commentService.sumbitComment(comment, video, userService.getUid(), originCommentId);
    }


    private void handleIntent() {
        Intent intent = getIntent();
        viewType = intent.getIntExtra(DetailFragmentImp.HEADER_VIEW_TYPE, PIECE_HEADER);
        video = (Video) getIntent().getSerializableExtra(DetailFragmentImp.VIDEO);
    }

    private void addDetailFragment() {
        mDetailFragment = new DetailFragmentImp();
        mDetailFragment.setOnCommentItemListener(this);
        Bundle bundle = getBundle(viewType);
        mDetailFragment.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.add(R.id.detail_layout, mDetailFragment);
        fragmentTransaction.commit();
    }

    private Bundle getBundle(int viewType) {
        Bundle bundle = new Bundle();
        bundle.putInt(DetailFragmentImp.HEADER_VIEW_TYPE, viewType);
        switch (viewType) {
            case VIDEO_HEADER:
                Video video = (Video) getIntent().getSerializableExtra(DetailFragmentImp.VIDEO);
                bundle.putSerializable(DetailFragmentImp.VIDEO, video);
                break;
        }
        return bundle;
    }

    @OnClick({R.id.iv_back})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaPlayerManager.getInstance().releaseMediaPlayer(MediaPlayerManager.PlayerTag.SHORT_VIDEO_DETAIL);
    }

    public static void startSelf(Context context, int viewType, Bundle bundle) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    private View getHeaderLayout() {
        MLog.debug(TAG, "getHeaderLayout headerType=" + viewType);
        switch (viewType) {
            case VIDEO_HEADER:
                DetailVideoHeaderView detailVideoHeaderView = new DetailVideoHeaderView(this);
                detailVideoHeaderView.updateData(video);
                return detailVideoHeaderView;
            case PIECE_HEADER:
                return new DetailPieceHeaderView(getContext());
            case FULL_TEXT_HEADER:
                return new DetailFullTextHeaderView(getContext());
            case INTRIGUE_HEADER:
                return new DetailIntrigueHeaderView(getContext());
            default:
                return new DetailVideoHeaderView(getContext());
        }
    }

    @Override
    public void onItemClick(int position, Comment comment) {
        if (userService.isLogin()) {
            showCommentDialog(position, comment);
        } else {
            LoginActivity.startActivity(this);
        }
    }


    private void showCommentDialog(final int position, final Comment comment) {
        if (mCommentDialog == null) {
            mCommentDialog = new CommentDialog(this);
            if (comment != null) {
                mCommentDialog.setContentHint("回复：" + comment.getUserName());
            }
            mCommentDialog.setOnSendButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.btn_send:
                            handleSendComment(v, position, comment);
                            break;
                    }

                }
            });
        }
        if (!mCommentDialog.isShowing()) {
            mCommentDialog.show();
        }
    }


    private void handleSendComment(View v, int position, Comment comment) {
        if (mCommentDialog != null) {
            onSend(mCommentDialog.getContent(), position, comment);
            mCommentDialog.dismiss();
            mCommentDialog = null;
        }

    }

}
